/*
 * @author: wizardc
 */

using System;
using Dou.ScreenController;
using UnityEditor;
using UnityEngine;
using Object = UnityEngine.Object;

namespace DouEditor.ScreenController
{
    public static class AdapterEditorUtil
    {
        public static T DrawObjectField<T>(T originObject, UIAdapterObject adapterObject, string label) where T : Object
        {
            var screenMode = ScreenControllerMgr.GetScreenMode();
            var fineTuningMode = ScreenControllerMgr.GetFineTuningMode();
            var uiScreenMode = (int) screenMode;
            var uiFineTuningMode = (int) fineTuningMode;
            
            if (adapterObject.GetValue(uiScreenMode, uiFineTuningMode, out var result))
            {
                originObject = (T) result;
            }
            var newObject = (T) EditorGUILayout.ObjectField(new GUIContent(label), originObject, typeof(T), false);
            if (newObject != originObject)
            {
                adapterObject.Record(screenMode, fineTuningMode, newObject);
            }

            return newObject;
        }

        public static bool DrawBoolField(bool originValue, UIAdapterBool adapterBool, string label)
        {
            var screenMode = ScreenControllerMgr.GetScreenMode();
            var fineTuningMode = ScreenControllerMgr.GetFineTuningMode();
            var uiScreenMode = (int) screenMode;
            var uiFineTuningMode = (int) fineTuningMode;

            if (adapterBool.GetValue(uiScreenMode, uiFineTuningMode, out var result))
            {
                originValue = result;
            }
            var newValue = EditorGUILayout.Toggle(new GUIContent(label), originValue);
            if (newValue != originValue)
            {
                adapterBool.Record(screenMode, fineTuningMode, newValue);
            }
            
            return newValue;
        }

        public static int DrawIntField(int originValue, UIAdapterInt adapterInt, string label)
        {
            var screenMode = ScreenControllerMgr.GetScreenMode();
            var fineTuningMode = ScreenControllerMgr.GetFineTuningMode();
            var uiScreenMode = (int) screenMode;
            var uiFineTuningMode = (int) fineTuningMode;

            if (adapterInt.GetValue(uiScreenMode, uiFineTuningMode, out var result))
            {
                originValue = result;
            }
            var newValue = EditorGUILayout.IntField(new GUIContent(label), originValue);
            if (newValue != originValue)
            {
                adapterInt.Record(screenMode, fineTuningMode, newValue);
            }
            
            return newValue;
        }

        public static float DrawFloatField(float originValue, UIAdapterFloat adapterFloat, string label)
        {
            var screenMode = ScreenControllerMgr.GetScreenMode();
            var fineTuningMode = ScreenControllerMgr.GetFineTuningMode();
            var uiScreenMode = (int) screenMode;
            var uiFineTuningMode = (int) fineTuningMode;

            if (adapterFloat.GetValue(uiScreenMode, uiFineTuningMode, out var result))
            {
                originValue = result;
            }
            var newValue = EditorGUILayout.FloatField(new GUIContent(label), originValue);
            if (newValue != originValue)
            {
                adapterFloat.Record(screenMode, fineTuningMode, newValue);
            }
            
            return newValue;
        }

        public static Color DrawColorField(Color originValue, UIAdapterColor adapterColor, string label)
        {
            var screenMode = ScreenControllerMgr.GetScreenMode();
            var fineTuningMode = ScreenControllerMgr.GetFineTuningMode();
            var uiScreenMode = (int) screenMode;
            var uiFineTuningMode = (int) fineTuningMode;

            if (adapterColor.GetValue(uiScreenMode, uiFineTuningMode, out var result))
            {
                originValue = result;
            }
            var newValue = EditorGUILayout.ColorField(new GUIContent(label), originValue);
            if (newValue != originValue)
            {
                adapterColor.Record(screenMode, fineTuningMode, newValue);
            }
            
            return newValue;
        }

        public static T DrawEnumField<T>(T originValue, UIAdapterEnum<T> adapterEnum, string label) where T : Enum
        {
            var screenMode = ScreenControllerMgr.GetScreenMode();
            var fineTuningMode = ScreenControllerMgr.GetFineTuningMode();
            var uiScreenMode = (int) screenMode;
            var uiFineTuningMode = (int) fineTuningMode;

            if (adapterEnum.GetValue(uiScreenMode, uiFineTuningMode, out var result))
            {
                originValue = result;
            }
            var newValue = (T) EditorGUILayout.EnumPopup(new GUIContent(label), originValue);
            if ((int)(object) newValue != (int)(object) originValue)
            {
                adapterEnum.Record(screenMode, fineTuningMode, newValue);
            }
            
            return newValue;
        }
    }
}
